﻿using Scoot.Services.Members;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Scoot.Controllers
{
    public class MemberController : Controller
    {
        private IMemberService members;

        public MemberController()
        {
            this.members = new MemberService();
        }

		public ActionResult Index()
		{
			return RedirectToAction("Active");
		}

        public ActionResult Active()
        {
            var model = members.Find(m => m.IsActive);
            return View(model);
        }

        public ActionResult Inactive()
        {
            var model = members.Find(m => !m.IsActive);
            return View(model);
        }

		public ActionResult Create()
		{
			var model = new MemberModel();
			model.IsActive = true;

			return View(model);
		}

		[HttpPost]
		public ActionResult Create(MemberModel model)
		{
			if (!ModelState.IsValid)
			{
				return View(model);
			}

			var result = members.Create(model);
			if (!result.WasSuccessful)
			{
				ModelState.MergeErrors(result.ValidationErrors);
				return View(model);
			}

			return RedirectToAction("Index");
		}

		public ActionResult Edit(Guid id)
		{
			var model = members.GetByID(id);
			return View(model);
		}

		[HttpPost]
		public ActionResult Edit(Guid id, MemberModel model)
		{
			if (!ModelState.IsValid)
			{
				return View(model);
			}

			var result = members.Edit(id, model);
			if (!result.WasSuccessful)
			{
				ModelState.MergeErrors(result.ValidationErrors);
				return View(model);
			}

			return RedirectToAction("Index");
		}
    }
}